import urllib.request, os, random
import urllib.parse
import ssl, string, urllib.parse as parse

ssl._create_default_https_context = ssl._create_unverified_context

ua = ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"]

class PSpider:
    def __init__(self):
        pass

    def request(self,url, refer="",encoding="utf-8"):
        nurl = parse.quote(url, safe=string.printable)
        nrefer=""
        if(refer):
            nrefer = parse.quote(refer, safe=string.printable)
        result = self.httpRequest(nurl, nrefer, encoding)
        return result

    def httpRequest(self,url, refer="", encoding="utf-8"):
        try:
            http_handler = urllib.request.HTTPHandler(debuglevel=1)
            opener = urllib.request.build_opener(http_handler)
            request = urllib.request.Request(url)
            if(refer):
                request.add_header("Referer",refer)
            request.add_header("User-Agent",random.choice(ua))
            response = opener.open(request,timeout=100)
            html = response.read().decode(encoding,"ignore")
            return html
        except Exception as err:
            print("error:"+str(err))
            return "error"